package cc.yugu.yhdw.webserver.service;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.AgentInfo;
import cc.yugu.yhdw.common.domain.basic.PassportInfo;
import cc.yugu.yhdw.common.domain.basic.SmsConfigInfo;
import cc.yugu.yhdw.common.domain.hdw.*;
import cc.yugu.yhdw.webserver.persistence.basic.AgentMapper;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import cc.yugu.yhdw.webserver.persistence.basic.SmsConfigMapper;
import cc.yugu.yhdw.webserver.persistence.basic.SystemConfigMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.*;
import com.whalin.MemCached.MemCachedClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
/**
 * 抽象类业务
 * 
 * @Date:Created in 2018/7/11 9:45
 * @Author:
 * @Modified By: zjn 2018/7/11 9:45
 */ 
public class AbstractService extends cc.yugu.yhdw.common.service.AbstractService {

    @Autowired
    MemCachedClient memCachedClient;

    @Autowired
    SmsConfigMapper smsConfigMapper;
    @Autowired
    PassportMapper passportMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    AgencyMapper agencyMapper;
    @Autowired
    AgentMapper agentMapper;
    @Autowired
    CabinetMapper cabinetMapper;
    @Autowired
    SystemConfigMapper systemConfigMapper;
    @Autowired
    CabinetCustomerMapper cabinetCustomerMapper;

    protected AgencyInfo findAgency(int id) {
        String key = CacheKey.key(CacheKey.K_ID_V_LEAGUE_INFO, id);
        AgencyInfo info = (AgencyInfo) memCachedClient.get(key);
        if (info != null) {
            return info;
        }
        info = agencyMapper.findInfo(id);
        if (info != null) {
            memCachedClient.set(key, info);
        }
        return info;
    }
    protected AgentInfo findAgentInfo(int id) {
        String key = CacheKey.key(CacheKey.K_ID_V_AGENT_INFO, id);
        AgentInfo info = (AgentInfo) memCachedClient.get(key);
        if (info != null) {
            return info;
        }
        info = agentMapper.findInfo(id);
        if (info != null) {
            memCachedClient.set(key, info);
        }
        return info;
    }
    /**
     * 根据客户id查询柜机id
     * @Param [customerId]
     * @Return java.util.List<java.lang.String>
     * @Author zxj 2018/7/20
     */
    protected List<String> findByCustomerId(Integer customerId) {
        return cabinetCustomerMapper.findByCustomerId(customerId);
    }

    protected String findConfigValue(String id) {
        return systemConfigMapper.findConfigValue(id);
    }

    protected List<String> findByAgent(Integer agentId) {
        return cabinetMapper.findByAgent(agentId);
    }

    protected SupplierInfo findSupplierInfo(int id) {
        //按照指定格式输出id
        String key = CacheKey.key(CacheKey.K_ID_V_SUPPLIER_INFO, id);
        SupplierInfo info = (SupplierInfo) memCachedClient.get(key);
        if (info != null) {
            return info;
        }
        info = supplierMapper.findInfo(id);
        if (info != null) {
            memCachedClient.set(key, info);
        }
        return info;
    }

    protected SmsConfigInfo findSmsConfigInfo(int id) {
        String key = CacheKey.key(CacheKey.K_ID_V_SMS_CONFIG_INFO, id);
        SmsConfigInfo info = (SmsConfigInfo) memCachedClient.get(key);
        if (info != null) {
            return info;
        }
        info = smsConfigMapper.findInfo(id);
        if (info != null) {
            memCachedClient.set(key, info);
        }
        return info;
    }

    protected PassportInfo findPassportInfo(int id) {
        String key = CacheKey.key(CacheKey.K_ID_V_PASSPORT_INFO, id);
        PassportInfo info = (PassportInfo) memCachedClient.get(key);
        if (info != null) {
            return info;
        }
        info = passportMapper.findInfo(id);
        if (info != null) {
            memCachedClient.set(key, info);
        }
        return info;
    }

    public Item findItem(int id) {
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM, id);
        Item item = (Item) memCachedClient.get(key);
        if (item != null) {
            return item;
        }
        item = itemMapper.find(id);
        if (item != null) {
            memCachedClient.set(key, item);
        }
        return item;
    }

    protected ItemEntity setItemProperties(ItemEntity entity) {
        Item item = findItem(entity.getItemId());
        setItemProperties(item, entity);
        return entity;
    }

    protected List<? extends ItemEntity> setItemProperties(List<? extends ItemEntity> list) {
        for (ItemEntity entity : list) {
            setItemProperties(entity);
        }

        return list;
    }

}
